/**
 * 使用 fcntl 更改终端为非阻塞状态
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

void set_file_status_flag(int fd, int flag)
{
    int val = fcntl(fd, F_GETFL);
    val |= flag;
    if (fcntl(fd, F_SETFL, val) < 0)
    {
        fprintf(stderr, "fcntl setfl error!");
    }
}



int main(){

    char buff[4096] = {0};

    set_file_status_flag(STDIN_FILENO,O_NONBLOCK);

    sleep(3);
    printf("Sleep finish!\n");
    int len = read(STDIN_FILENO,buff,4096);
    

    if(len < 0){
        fprintf(stderr,"Read error!\n");
        exit(EXIT_FAILURE);
    }
    else if(len > 0){
        write(STDOUT_FILENO,buff,len);
    }
    else{
        fprintf(stderr,"Read Zero!\n");
    }
    

    return 0;
}